package com.glorysoft.lipuxinpda.base

import android.app.Activity
import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.glorysoft.lipuxinpda.R
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils

/**
 * FileName: BaseActivty
 * Author: XJH
 * Date: 2021/4/8 14:57
 * Description:
 * History:
 */
abstract class BaseActivity : FreeUI_BaseActivity<Any?>(){

    var alertDialog: FreeUI_GeneralFragmentDialog? = null

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        // 当按下返回键时所执行的命令
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            showBackDialog(this)
            false
        } else {
            super.onKeyDown(keyCode, event)
        }
    }

    protected fun showBackDialog(context: Context) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(
                this, "确定要退出当前界面吗？", { (context as Activity).finish() }) {

        }.show(supportFragmentManager, "DIALOG")
    }
    protected open fun showDialog(message: String?, result: Boolean) {
        alertDialog = FreeUI_GeneralFragmentDialog()
        val view: View = LayoutInflater.from(App.app).inflate(R.layout.dialog_prompt, null)
        val tv = view.findViewById<TextView>(R.id.tvTips)
        val btnConfirm = view.findViewById<Button>(R.id.btnConfirm)
        btnConfirm.setOnClickListener { alertDialog!!.dismiss() }
        tv.text = message
        alertDialog!!.setLayout(view)
        alertDialog!!.show(supportFragmentManager, "prompt")
        alertDialog!!.setCallBack(FreeUI_CommonCallback { alertDialog = null })
    }
}